<?php


namespace addon\hotel\hotel\controller;


use addon\hotel\model\RoomType as RoomTypeModel;

class Roomtype extends BaseHotel
{
    //房间列表
    public function lists()
    {

        if (request()->isAjax()){

            $room_model = new RoomTypeModel();

            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);

            $search_text = input('search_text','');

            $condition = [
                ['hotel_id','=',$this->hotel_id]
            ];

            if ($search_text) {
                $condition[] = [ 'type_name', 'like', '%' . $search_text . '%' ];
            }

            $list = $room_model->getRoomTypePageList($condition, $page, $page_size, 'type_id desc');

            return $list;
        }

        return $this->fetch('roomtype/lists', [], $this->replace);
    }

    //添加房间
    public function add()
    {
        $room_model = new RoomTypeModel();

        if (request()->isAjax()){

            $data = [
                'type_name' => input('type_name', ''),
                'hotel_id' => $this->hotel_id,
                'facilities_json' => input('facilities', ''),
                /*'occupancy' => input('occupancy', 0),
                'bed' => input('bed', ''),
                'area' => input('area', ''),
                'floor' => input('floor', 0),
                'breakfast' => input('breakfast', 0),
                'bansmoking' => input('bansmoking', 0),
                'wifi' => input('wifi', 0),
                'broadband' => input('broadband', 0),
                'window' => input('window', 0),*/
                'create_time' => time(),
            ];

            $res = $room_model->addRoomType($data);
            return $res;

        }

        $facilities = $room_model->facilities;
        $this->assign('facilities',$facilities);

        return $this->fetch('roomtype/add', [], $this->replace);
    }

    //编辑房间
    public function edit()
    {
        $type_id = input("type_id", 0);

        $room_model = new RoomTypeModel();
        if (request()->isAjax()){

            $data = [
                'type_name' => input('type_name', ''),
                'facilities_json' => input('facilities', ''),
                'modify_time' => time(),
            ];

            $type_id = input("type_id", 0);

            $condition = array (
                [ "type_id", "=", $type_id ]
            );

            $res = $room_model->editRoomType($data,$condition);
            return $res;

        }
        $info = $room_model->getRoomTypeInfo($type_id);

        if ($info['data']['facilities_json']){
            $info['data']['facilities_json'] = json_decode($info['data']['facilities_json'],true);
        } else {
            $info['data']['facilities_json'] = $room_model->facilities;
        }

        $this->assign('info',$info['data']);

        return $this->fetch('roomtype/edit', [], $this->replace);
    }

    public function delete()
    {
        if (request()->isAjax()) {
            $type_ids = input('type_ids', 0);
            $room_model = new RoomTypeModel();
            return $room_model->deleteRoomType([ [ 'type_id', 'in', $type_ids ] ]);
        }
    }
}